import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = 'sales/api/v1';
import * as dto from './DTO';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询总代整车信息'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询详细'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/dealerSaleInformation/init`, null, dto.init)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询列表
export const getList = bodyCondition => {
    const queryString = stringify(bodyCondition);
    return request(`${baseUrl}/dealerSaleInformations/forCurrentUser?${queryString}`, null, dto.list)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询总代整车详细信息
export const getDetail = id => request(`${baseUrl}/dealerSaleInformations/${id}`, null, dto.detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 编辑总代整车详细信息
export const updateData = (id, data) => request(`${baseUrl}/dealerSaleInformations/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 物流周期设置
export const settingData = (id, data) => request(`${baseUrl}/dealerSaleInformations/${id}/setting`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
